import http from './http'
import type {AttachListItem} from './attachAPI'

export interface SendMailResponse {
  code: number
  msg: string
  email_id?: number
}

export interface MailListItem {
  id: number
  subject: string
  sender: string
  is_read: boolean
  received_at: string
  status: string
  has_attachments: boolean
  receiver?: string
  cc?: string[]
}

export interface MailListResponse {
  code: number
  msg: string
  total: number
  data: MailListItem[]
}

export interface MailDetailResponse {
  code: number
  msg: string
  data: {
    id: number
    subject: string
    body: string
    sender: string
    status: string
    sent_at: string
    attachments: AttachListItem[]
    receiver: string
    is_read: boolean
    received_at: string
    cc: string[]
  }
}

export interface MarkReadResponse {
  code: number
  msg: string
}

export interface DeleteMailResponse {
  code: number
  msg: string
}

export interface SearchMailItem {
  id: number
  subject: string
  sender: string
  is_read: boolean
  received_at: string
}

export interface SearchMailResponse {
  code: number
  msg: string
  data: SearchMailItem[]
}

export interface DraftResponse {
  code: number
  msg: string
  data: {
    draft_id: number
  }
}

export interface SendDraftResponse {
  code: number
  msg: string
  email_id: number
}

// 发送邮件
export const sendMailAPI = async (params: {
  receiver: string
  cc: string[]
  subject: string
  content: string
  attachments: number[]
}): Promise<SendMailResponse> => {
  return await http.post('/mail/send', params)
}

// 获取邮件列表
export const getMailListAPI = async (folder: string): Promise<MailListResponse> => {
  return await http.get(`/mail/list?folder=${folder}`)
}

// 获取邮件详情
export const getMailDetailAPI = async (mail_id: number): Promise<MailDetailResponse> => {
  return await http.get(`/mail/detail/${mail_id}`)
}

// 邮件标记已读
export const markMailReadAPI = async (mail_id: number): Promise<MarkReadResponse> => {
  return await http.put(`/mail/read/${mail_id}`, { status: 'read' })
}

// 删除邮件
export const deleteMailAPI = async (ids: number[]): Promise<DeleteMailResponse> => {
  return await http.delete('/mail/delete', { data: { ids } })
}

// 邮件搜索
export const searchMailAPI = async (keyword: string): Promise<SearchMailResponse> => {
  return await http.post('/mail/search', { keyword })
}

// 创建草稿邮件
export const createDraftAPI = async (params: {
  receiver: string
  cc: string[]
  subject: string
  content: string
  attachments: number[]
  send_now: boolean
}): Promise<DraftResponse> => {
  return await http.post('/mail/draft', params)
}

// 更新草稿邮件
export const updateDraftAPI = async (
  mail_id: number,
  params: {
    receiver: string
    cc: string[]
    subject: string
    content: string
    attachments: number[] //更新要给id
  },
): Promise<DraftResponse> => {
  return await http.put(`/mail/draft/${mail_id}`, params)
}

// 发送草稿邮件
export const sendDraftAPI = async (
  draft_id: number,
): Promise<SendDraftResponse> => {
  return await http.post(`/mail/draft/${draft_id}/send`)
}
